//
//  CZHomeTableViewCell.swift
//  git
//
//  Created by 徐 鹏超 on 16/4/10.
//  Copyright © 2016年 徐 鹏飞. All rights reserved.
//

import UIKit
import SnapKit


class CZHomeTableViewCell: UITableViewCell {
    // 定义一个关联约束
    var constant: Constraint?
    
    var statusModel: CZStatusModel?{
        didSet{
            
            orginalView.orginalStatusModel = statusModel
            
            // 给视图传值
            footerView.footerStatusModel = statusModel
            
            constant?.uninstall()
            
            
            if statusModel?.retweeted_status == nil {
                printLog("原创微博")
                retweetView.hidden = true
                
                // 再更新约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    // 直接通过 make.top.equalTo(orginalView.snp_bottom).constraint 就可以获得到 顶部约束
                    
                    constant =  make.top.equalTo(orginalView.snp_bottom).constraint
                })
                
                
            } else{
                printLog("转发微博")
                retweetView.hidden = false
                //
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    constant =  make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                
                retweetView.retweetStatus = statusModel
            }


        }
        
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        // 开始做cell的时候,可以先让这个原创微博填充整个视图
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        orginalView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(contentView.snp_bottom)
            
        }
        // 设置footer约束
        footerView.snp_makeConstraints { (make) -> Void in
            constant = make.top.equalTo(retweetView.snp_bottom).constraint
//            make.top.equalTo(retweetView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //            make.bottom.equalTo(contentView.snp_bottom)
            
            make.height.equalTo(44)
        }
        
        // 1.自动计算行高 需要contentView 告诉 cell它的边界,特别是 底部
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            // 关键是底部,因为自动计算行高,就是看的底部到哪里
            make.bottom.equalTo(footerView.snp_bottom)
        }
        
        // 转发微博的约束: 左边:上边是原创微博的底部;右边; 底部是 footerView 的顶部
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
            
        }


        
    }
    
    
    //MARK:--懒加载
    
    private lazy var orginalView: CZOrginalView = CZOrginalView()
    
    private lazy var retweetView: CZRetweetView = CZRetweetView()
    
    private lazy var footerView: CZFooterView = CZFooterView()

}
